package com.redhat.redwork.widget.tree;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

/**
 * tree组件
 * @author tony.tan
 * @version 
 *      增加节点{@link TreeNode}拓展功能(如 {@link TreeNodeCkBox})<br />
 *      by:vakin.jiang at 2010.04.27
 */
public class TreeAction extends ActionSupport {

	
	private static final long serialVersionUID = -6679029662650203103L;

	private Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
	private TreeService treeService;
	
	private String treeModelBean;

	private String id; // 节点ID

	@NotNull
	private String rootId; // 根节点ID

	private String href; // 节点URL
	
	/*////////////增加是否需要复选框按钮拓展(	vakin.jiang at 2010.04.27)///////////*/
	
	//是否需要复选框按钮 1：true，0：false
	private Integer withCheckBox = 0;

	private List<? extends TreeNode> treeData;

	private Class<? extends TreeNode> getTreeNodeClass(){
		if(new Integer("1").equals(withCheckBox))
			return TreeNodeCkBox.class;
		return TreeNode.class;
	}
	/*/////////////end//////////*/
	
	@Override
	public String execute() {
		try {
			if (StringUtils.isBlank(id)) {
				id = rootId;
			}
			treeData = treeService.loadTreeNodeData(getTreeNodeClass(),treeModelBean,id,href);
		} catch (Exception e) {
			logger.error(e);
		}

		return SUCCESS;
	}

	public String getRootId() {
		return rootId;
	}

	public void setRootId(String rootId) {
		this.rootId = rootId;
	}

	public void setTreeModelBean(String treeModelBean) {
		this.treeModelBean = treeModelBean;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<? extends TreeNode> getTreeData() {
		return treeData;
	}

	public void setTreeData(List<? extends TreeNode> treeData) {
		this.treeData = treeData;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public void setTreeService(TreeService treeService) {
		this.treeService = treeService;
	}

	public Integer getWithCheckBox() {
		return withCheckBox;
	}

	public void setWithCheckBox(Integer withCheckBox) {
		this.withCheckBox = withCheckBox;
	}
	
	
}
